package com.cbsd.compress;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;

import com.zhihu.matisse.R;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import top.zibin.luban.Luban;

/**
 * 压缩Activity
 *
 * @author zhaoyang
 */
public class CompressActivity extends AppCompatActivity {


    /**
     * 压缩图片
     *
     * @param activity
     * @param oriFile
     * @param requestCode
     */
    public static void compress(Activity activity, String oriFile, int requestCode) {
        compress(activity, Arrays.asList(new String[]{oriFile}), requestCode);
    }

    /**
     * 压缩图片
     *
     * @param activity
     * @param oriFileList
     * @param requestCode
     */
    public static void compress(Activity activity, String[] oriFileList, int requestCode) {
        compress(activity, Arrays.asList(oriFileList), requestCode);
    }

    /**
     * 压缩图片
     *
     * @param activity
     * @param oriFileList
     * @param requestCode
     */
    public static void compress(Activity activity, List<String> oriFileList, int requestCode) {
        ArrayList oriUriArraysList = new ArrayList<>(oriFileList);
        Intent intent = new Intent(activity, CompressActivity.class);
        intent.putStringArrayListExtra("param1", oriUriArraysList);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 获取返回结果
     *
     * @param intent
     * @return
     */
    public static ArrayList<String> obtainResult(Intent intent) {
        return intent.getStringArrayListExtra("result");
    }

    private ArrayList<String> oriList;
    private ArrayList<String> compressList = new ArrayList<>();
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.matisse_activity_compress);
        setTitle("");
        progressBar = ((ProgressBar) findViewById(R.id.progressBar));
        oriList = getIntent().getStringArrayListExtra("param1");
        compressFile();
    }

    private static final int START = 1, COMPRESSING = 2, END = 3, ERROR = 4, COMPRESS_SUCCESS = 5;
    private Handler mHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case START:
                    progressBar.setMax(oriList.size());
                    break;
                case COMPRESSING:
                    progressBar.setProgress(msg.arg1);
                    break;
                case COMPRESS_SUCCESS:
                    String resultFilePath = ((String) msg.obj);
                    compressList.add(resultFilePath);
                    break;
                case END:
                    progressBar.setVisibility(View.INVISIBLE);
                    Intent intent = new Intent();
                    intent.putStringArrayListExtra("result", compressList);
                    CompressActivity.this.setResult(RESULT_OK, intent);
                    CompressActivity.this.finish();
                    break;
                case ERROR:
                    progressBar.setVisibility(View.INVISIBLE);
                    CompressActivity.this.setResult(RESULT_CANCELED);
                    CompressActivity.this.finish();
                    break;
            }
        }
    };

    public void compressFile() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    mHandler.obtainMessage(START).sendToTarget();
                    for (int i = 0; i < oriList.size(); i++) {
                        mHandler.obtainMessage(COMPRESSING, i + 1, oriList.size()).sendToTarget();
                        File file = new File(oriList.get(i));
                        File resultFile = Luban.with(CompressActivity.this).load(file).get();
                        mHandler.obtainMessage(COMPRESSING, i + 1, oriList.size(), resultFile.getAbsolutePath()).sendToTarget();
                    }
                    mHandler.obtainMessage(END).sendToTarget();
                    Thread.sleep(1000);
                } catch (Exception exception) {
                    mHandler.obtainMessage(ERROR).sendToTarget();
                }
            }

        });
        thread.start();
    }

    @Override
    public void onBackPressed() {

    }
}
